﻿using AppManager.Model;
using System.Runtime.Remoting.Messaging;
using AppManager.DomainModel;
using AutoMapper;
using AppManager.IBLL;

namespace AppManager.BLL
{
    public class Sys_MenuService : BaseService<Sys_Menu>, IMenuService
    {
        public static Sys_MenuService Current
        {
            get
            {
                var name = typeof(Sys_MenuService).Name;
                var bll = CallContext.GetData(name) as Sys_MenuService;
                if (bll != null)
                {
                    return bll;
                }
                bll = new Sys_MenuService();
                CallContext.SetData(name, bll);
                return bll;
            }
        }

        public int AddOrUpdate(Sys_MenuEidtModel model)
        {
            var menu = BaseService<Sys_Menu>.Current.GetModel(s => s.Id == model.Id);
            if (menu == null)
            {
                menu = Mapper.Map<Sys_Menu>(model);
            }
            else
            {
                //还是不能使用AutoMapper,因为有些参数其实是不能修改的
                menu.Flag = model.Flag;
                menu.Link = model.Link?.TrimStart('/')?.TrimEnd('/');
                menu.Name = model.Name;
                menu.ParentId = model.ParentId;
            }

            return AddOrUpdate(menu);
        }
    }
}
